package model;

import java.sql.*;

/**
 * Created by yvdedu.com on 2016/9/21.
 */
public class Jdbc {
    private String driver;
    private String conStr;
    private String name;
    private String pwd;

    public String getDriver() {
        return driver;
    }

    public String getConStr() {
        return conStr;
    }

    public String getName() {
        return name;
    }

    public String getPwd() {
        return pwd;
    }

    private Connection connection = null;
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;


    private void prepared(String sql, Object... params) throws SQLException {
        closePreparedStatement();
        preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i + 1, params[i]);
        }

    }

    private void closeResultSet() {
        try {
            if (resultSet != null) resultSet.close();
        } catch (SQLException e) {
            resultSet = null;
            e.printStackTrace();
        }
    }

    private void closePreparedStatement() {
        try {
            if (preparedStatement != null) preparedStatement.close();
        } catch (SQLException e) {
            preparedStatement = null;
            e.printStackTrace();
        }
    }

    private void closeConnection() {
        try {
            if (connection != null) connection.close();
        } catch (SQLException e) {
            connection = null;
            e.printStackTrace();
        }
    }


    public Jdbc(String driver, String conStr, String name, String pwd) {
        try {
            Class.forName(driver);
            this.driver = driver;
            this.conStr = conStr;
            this.name = name;
            this.pwd = pwd;
            connection = DriverManager.getConnection(conStr, name, pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public int execUpdate(String sql, Object... params) {
        int row = 0;
        try {
            prepared(sql, params);
            row = preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return row;
    }

    public <T> T execQuery(packEntity<T> packEntity, String sql, Object... params) {
        try {
            prepared(sql, params);
            closeResultSet();
            resultSet = preparedStatement.executeQuery();
            return packEntity.pack(resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public ResultSet execQueryToResult(String sql, Object... params) {
        try {
            prepared(sql, params);
            closeResultSet();
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }

    @Override
    protected void finalize() throws Throwable {
        closeResultSet();
        closePreparedStatement();
        closeConnection();
        super.finalize();
    }

    static interface packEntity<T> {
        T pack(ResultSet resultSet);
    }
}

